/**
 * 想服务器获取数据的定义接口，静态HTML网页返回成功之后，
 * 向服务器获取数据，返回为json格式的，数据返回成功之后在进行加载
 *
 * 特别注意json解析不成功将不能返回结果，并没有提示
 */


/**
 * 解析URL中的参数
 * @param link
 * @returns {{}}
 */
function parseParams(link) {
    var paramMap = {};
    if (link.length > 1) {
        var paramStr = link[1].split("&");
        for (var i = 0; i < paramStr.length; i++) {
            var param = paramStr[i].split("=");
            if (param.length === 2) {
                paramMap[param[0]] = param[1]
            }
        }
    }
    return paramMap;
}


/**
 * 向服务器获取数据，然后解析template模板
 * @param content
 * @param next
 * @param url
 */
function queryAndParseTemplate(content, next, url) {
    if (url === undefined || url === '') {
        var template = Template7.compile(content);
        var resultContent = template();
        next(resultContent);
    } else {
        $$.getJSON(url, function (data) {
            var template = Template7.compile(content);
            var resultContent = template(data);
            next(resultContent);
        })
    }
}

/**
 * ajax加载页面返回
 * @param content
 * @param url
 * @param next
 * @returns {*}
 */
var preProcess = function parsePages(content, url, next) {

    var link = url.split("?");
    url = link[0];
    var paramMap = parseParams(link);

    console.log('params: ', paramMap);

    console.log('url: ', url, ' \n params: ', paramMap);

    switch (url) {
        case  './index/eat.html':
            indexEat(content, next, paramMap);
            break;
        case './index/play.html':
            indexPlay(content, next);
            break;
        case './index/user.html':
            queryAndParseTemplate(content, next, '../static/custom/data/user.json');
            break;
        case './user/user-info.html':
            queryAndParseTemplate(content, next);
            break;
        case './user/credit.html':
            queryBalanceAndCredit(content, next, paramMap);
            break;
        case './goods/detail.html':
            queryGoodsDetail(content, next, paramMap);
            break;
        case './order/order.html':
            getOrders(content, next, paramMap['page']);
            break;  //订单中心的界面
        case './order/detail.html':
            queryOrderDetails(content, next, paramMap);
            break;
        case './order/to-order.html':
            queryOrderData(content, next, paramMap);
            break;
        case './goods/pay-page.html':
            queryAndParseTemplate(content, next, '../static/custom/data/order-pay-detail.json');
            break;
        default:
            return content
    }


};

/*首页吃遍四川*/
function indexEat(content, next, paramMap) {
    $$.getJSON('../static/custom/data/home-eat.json', function (data) {
        var template = Template7.compile(content);
        var resultContent = template(data);
        // Now we call "next" callback function with result content
        next(resultContent);
    })
}

function indexPlay(content, next) {
    $$.getJSON('../static/custom/data/home-play.json', function (data) {
        var template = Template7.compile(content);
        var resultContent = template(data);
        // Now we call "next" callback function with result content
        next(resultContent);
    })
}

/**
 * 查看商品详情
 * @param content
 * @param next
 * @param paramMap
 */
function queryGoodsDetail(content, next, paramMap) {
    var id = paramMap['id'];
    console.log('id: ', id);
    queryAndParseTemplate(content, next, '../static/custom/data/goods-detail.json');
}


/**
 * 订单中心的界面，因为要切换tab，所有 手动更改数据中的page 值
 * @param content
 * @param next
 * @param index
 */
function getOrders(content, next, index) {
    if (index === undefined)
        index = 1;

    $$.getJSON('../static/custom/data/orders.json', function (data) {
        data.page = index;
        console.log('data: ', data);
        var template = Template7.compile(content);
        // Compile content template with received JSON data
        var resultContent = template(data);
        // Now we call "next" callback function with result content
        next(resultContent);
    });
}

/**
 * 实际上我这里多加了一个参数，type，订单的状态类型，用来返回不同的数据，
 * 实际中不需要
 * @param content
 * @param next
 * @param paramMap
 */
function queryOrderDetails(content, next, paramMap) {
    var id = paramMap['id'];
    var type = paramMap['type'];
    console.log('id: ', id, '  type: ', type);
    $$.getJSON('../static/custom/data/order-detail.json', function (data) {

        console.log('data: ', data);
        var template = Template7.compile(content);
        // Compile content template with received JSON data
        var order;
        if (type === '1') {
            order = data.pay;
        } else if (type === '2') {
            order = data.orders;
        } else if (type === '3') {
            order = data.consume;
        } else if (type === '4') {
            order = data.completed;
        }

        console.log('order-detail: ', order);
        var resultContent = template(order);
        // Now we call "next" callback function with result content
        next(resultContent);
    });
}

/**
 * 进入预约界面，查询预约的数据
 * @param content
 * @param next
 * @param paramMap
 */
function queryOrderData(content, next, paramMap) {
    next(content)
}

/**
 * 插叙积分和收益
 */
function queryBalanceAndCredit(content, next, paramMap) {
    var type = paramMap['type'];
    if (type === undefined) {
        type = 1;
    }
    console.log('dataJson type : ',type);
    $$.getJSON('../static/custom/data/balance.json', function (data) {
        data.type = type;
        var template = Template7.compile(content);
        var resultContent = template(data);
        next(resultContent);
    });
}

